const path = require('path');
const Registry = require('winreg');
let copyToolsList = require('./data/copyToolsList');
let batToolsList = require('./data/batToolsList');
let fileToolsList = require('./data/fileToolsList');
let dirToolsList = require('./data/dirToolsList');
let nodePath = path.join(process.env.NVM_SYMLINK, 'node.exe');
let copyPath = path.join(__dirname, 'commands', 'copy.js');
const methodToPromise = function (host, method, params) {
  return new Promise((resolve, reject) => {
    if (!host || !method) {reject('宿主名和方法名不能为空')}
    if (params) {
      if (!(params instanceof Array)) {params = [params]}
      host[method](...params, function (err) {
        if (err) {reject(err)}
        else {resolve(...arguments)}
      });
    } else {
      host[method](function (err) {
        if (err) {reject(err)}
        else {resolve(...arguments)}
      });
    }
  })
}
let ret_bat = (async function () {
  let regKey = new Registry({
    hive: Registry.HKCR,
    key: '\\Directory\\Background\\shell\\批处理便捷工具'
  });
  let regVal = '';
  batToolsList.forEach(v => { regVal += `${v.value};`; });
  await methodToPromise(regKey, 'create');
  // create方法创建的“(默认)”值不是“(数值未设置)”，需要用clear清空。
  await methodToPromise(regKey, 'clear');
  regKey.set('SubCommands', 'REG_SZ', regVal, function (err) {
    if (err) {console.log(err)}
  });
  batToolsList.forEach(async (v) => {
    let batPath = path.join(__dirname, 'commands', v.file);
    let regKey1 = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}`
    });
    let regKey2 = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}\\command`
    });
    let regVal1 = v.label;
    let regVal2 = `\"\\\"${batPath}\\\"\"`;
    await methodToPromise(regKey1, 'create');
    await methodToPromise(regKey2, 'create');
    await methodToPromise(regKey1, 'set', [Registry.DEFAULT_VALUE, 'REG_SZ', regVal1]);
    await methodToPromise(regKey2, 'set', [Registry.DEFAULT_VALUE, 'REG_SZ', regVal2]);
  });
  console.log('[安装完成] 批处理便捷工具');
})();
let ret_copy = (async function () {
  let regKey = new Registry({
    hive: Registry.HKCR,
    key: '\\Directory\\Background\\shell\\前端便捷工具'
  });
  let regVal = '';
  copyToolsList.forEach(v => { regVal += `${v.value};`; });
  await methodToPromise(regKey, 'create');
  // create方法创建的“(默认)”值不是“(数值未设置)”，需要用clear清空。
  await methodToPromise(regKey, 'clear');
  regKey.set('SubCommands', 'REG_SZ', regVal, function (err) {
    if (err) {console.log(err)}
  });
  copyToolsList.forEach(async (v) => {
    let regKey1 = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}`
    });
    let regKey2 = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}\\command`
    });
    let regVal1 = `引入${v.label}`;
    let regVal2 = `\"\\\"${nodePath}\\\" \\\"${copyPath}\\\" ${v.file} \\\"%V\\\"\"`;
    await methodToPromise(regKey1, 'create');
    await methodToPromise(regKey2, 'create');
    await methodToPromise(regKey1, 'set', [Registry.DEFAULT_VALUE, 'REG_SZ', regVal1]);
    await methodToPromise(regKey2, 'set', [Registry.DEFAULT_VALUE, 'REG_SZ', regVal2]);
  });
  console.log('[安装完成] 文件夹空白处·前端便捷工具');
})();
let ret_file = (async function () {
  let regKey = new Registry({
    hive: Registry.HKCR,
    key: '\\*\\shell\\XTools'
  });
  let regVal = '';
  fileToolsList.forEach(v => { regVal += `${v.value};`; });
  await methodToPromise(regKey, 'create');
  // create方法创建的“(默认)”值不是“(数值未设置)”，需要用clear清空。
  await methodToPromise(regKey, 'clear');
  regKey.set('MUIVerb', 'REG_SZ', '前端便捷工具', function (err) {
    if (err) {console.log(err)}
  });
  regKey.set('SubCommands', 'REG_SZ', regVal, function (err) {
    if (err) {console.log(err)}
  });
  fileToolsList.forEach(async (v) => {
    let filePath = path.join(__dirname, 'commands', v.file);
    let regKey1 = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}`
    });
    let regKey2 = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}\\command`
    });
    let regVal1 = v.label;
    let regVal2 = `\"\\\"${nodePath}\\\" \\\"${filePath}\\\" \\\"%1\\\"\"`;
    await methodToPromise(regKey1, 'create');
    await methodToPromise(regKey2, 'create');
    await methodToPromise(regKey1, 'set', [Registry.DEFAULT_VALUE, 'REG_SZ', regVal1]);
    await methodToPromise(regKey2, 'set', [Registry.DEFAULT_VALUE, 'REG_SZ', regVal2]);
  });
  console.log('[安装完成] 文件·前端便捷工具');
})();
let ret_dir = (async function () {
  let regKey = new Registry({
    hive: Registry.HKCR,
    key: '\\Directory\\shell\\前端便捷工具'
  });
  let regVal = '';
  dirToolsList.forEach(v => { regVal += `${v.value};`; });
  await methodToPromise(regKey, 'create');
  // create方法创建的“(默认)”值不是“(数值未设置)”，需要用clear清空。
  await methodToPromise(regKey, 'clear');
  regKey.set('SubCommands', 'REG_SZ', regVal, function (err) {
    if (err) {console.log(err)}
  });
  dirToolsList.forEach(async (v) => {
    let filePath = path.join(__dirname, 'commands', v.file);
    let regKey1 = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}`
    });
    let regKey2 = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}\\command`
    });
    let regVal1 = v.label;
    let regVal2 = `\"\\\"${nodePath}\\\" \\\"${filePath}\\\" \\\"%1\\\"\"`;
    await methodToPromise(regKey1, 'create');
    await methodToPromise(regKey2, 'create');
    await methodToPromise(regKey1, 'set', [Registry.DEFAULT_VALUE, 'REG_SZ', regVal1]);
    await methodToPromise(regKey2, 'set', [Registry.DEFAULT_VALUE, 'REG_SZ', regVal2]);
  });
  console.log('[安装完成] 文件夹·前端便捷工具');
})();
